﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;

namespace VitCode.ObjectResolutionHelper
{
    public class LookUpClassHelper
    {
        
        /// <summary>
        /// 获取类集体
        /// </summary>
        /// <param name="strAssName">指定程序集名称</param>
        /// <param name="strNaming">指定命名空间下[必须是程序集中存在的]</param>
        /// <returns></returns>
        public static ArrayList GetTypes(string strAssName, string strNaming)
        {
            Assembly assObj = Assembly.Load(strAssName);
            var al = new ArrayList();
            Type[] typeObjs = assObj.GetTypes();
            foreach (Type t in typeObjs)
            {
                if (t.Namespace == strNaming)
                    al.Add(t);
            }
            return al;
        }


        public static PropertyInfo[] GetPros(Type objType)
        {
            var p = objType.GetProperties();
            return p;
        }

        public static string GetOrMemoDesValue(Type type)
        {
            var objVal =  GetOrDesVal(type, typeof(OrMemoAttribute));
            return objVal == null ? "" : objVal.ToString();
        }

        public static string GetOrMemoDesValue(PropertyInfo proInfo)
        {
            var objVal = GetOrDesVal(proInfo, typeof(OrMemoAttribute));
            return objVal == null ? "" : objVal.ToString();
        }

        public static object GetOrDesVal(Type type,Type attrType)
        {
            object reVal = null;

            if (type != null)
            {
                var desAttrObj = Attribute.GetCustomAttribute(type, attrType);
                if (desAttrObj != null)
                {
                    var pro = attrType.GetProperty("Description");
                    if (pro != null)
                        reVal = pro.GetValue(desAttrObj, null);
                }
            }
            return reVal;
        }

        public static object GetOrDesVal(PropertyInfo proInfo, Type attrType)
        {
            object reVal=null;

            if (proInfo != null)
            {
                var desAttrObj = Attribute.GetCustomAttribute(proInfo, attrType);
                if (desAttrObj != null)
                {
                    var pro = attrType.GetProperty("Description");
                    if (pro != null)
                        reVal = pro.GetValue(desAttrObj, null);
                }
            }
            return reVal;
        }

        

        public static string ReplaceHistoryWord(string strOrg)
        {
            return strOrg.Replace(".Core", ".History");
        }

        public static string GetAssName(Type t)
        {
            return t.Assembly.FullName.Substring(0, t.Assembly.FullName.IndexOf(','));
        }

        public static Type GetHisType(Type baseObj)
        {
            var assName = GetAssName(baseObj);
            var hisAssName = ReplaceHistoryWord(assName);
            var hisTypeFullName = ReplaceHistoryWord(baseObj.ToString())+HistoryKeyInfo.LAST_STRING;
            var assObj = Assembly.Load(hisAssName);
            var reType = assObj.GetType(hisTypeFullName,false);
            return reType;
        }


        public static string ConvertStringValue(StringCollection sc)
        {
            var sb = new StringBuilder();
            for (var i = 0; i < sc.Count; i++)
            {
                if (sb.Length > 0)
                    sb.Append(",");
                sb.Append(sc[i]);
            }
            return sb.ToString();
        }
    }
}
